package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.dao.SaleListDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SaleListService;
import com.atguigu.jxc.util.DateUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author WZhaokang
 * @date 2020/12/30 1:27
 */
@Service
public class SaleListServiceImpl implements SaleListService {

    @Autowired
    private SaleListDao saleListDao;
    @Autowired
    private SaleListGoodsDao saleListGoodsDao;
    @Autowired
    private GoodsDao goodsDao;

    @Override
    @Transactional
    public void save(SaleList saleList, String saleListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        saleList.setUserId(user.getUserId());
        saleListDao.save(saleList);

        SaleListGoods[] saleListGoods = new Gson().fromJson(saleListGoodsStr, SaleListGoods[].class);
        List<SaleListGoods> saleListGoodsList = Arrays.asList(saleListGoods);
        for (SaleListGoods listGoods : saleListGoodsList) {
            Integer saleListId = saleList.getSaleListId();
            Integer goodsId = listGoods.getGoodsId();
            Goods goods = goodsDao.findByGoodsId(goodsId);
            goods.setInventoryQuantity(listGoods.getGoodsNum() + goods.getInventoryQuantity());
            goodsDao.updateGoods(goods);
            listGoods.setSaleListId(saleListId);
            saleListGoodsDao.save(listGoods);
        }
    }

    @Override
    public List<SaleList> list(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {
        return saleListDao.list(saleNumber,customerId,state,sTime,eTime);
    }

    @Override
    public void delete(Integer saleListId) {
        saleListGoodsDao.delete(saleListId);
        saleListDao.delete(saleListId);
    }

    @Override
    public void updateState(Integer saleListId) {
        saleListDao.updateState(saleListId,1);
    }

    @Override
    public List<PurchaseVO2> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        return saleListDao.count(sTime,eTime,goodsTypeId,codeOrName);
    }

    @Override
    public List<Map> getSaleDataByDay(String sTime, String eTime) {
        List<Map> saleDataByDay = saleListDao.getSaleDataByDay(sTime, eTime);
        for (Map map : saleDataByDay) {
            /*System.out.println(map.get("date"));
            System.out.println(map.get("saleTotal"));
            System.out.println(map.get("purchasingTotal"));*/
            Double saleTotal = (Double) map.get("saleTotal");
            Double purchasingTotal = (Double) map.get("purchasingTotal");
            map.put("profit", saleTotal - purchasingTotal);
        }

        return saleDataByDay;
    }

    @Override
    public List<Map> getSaleDataByMonth(String sTime, String eTime) {
        List<Map> getSaleDataByMonth = saleListDao.getSaleDataByMonth(sTime, eTime);
        for (Map map : getSaleDataByMonth) {
            /*System.out.println(map.get("date"));
            System.out.println(map.get("saleTotal"));
            System.out.println(map.get("purchasingTotal"));*/
            Double saleTotal = (Double) map.get("saleTotal");
            Double purchasingTotal = (Double) map.get("purchasingTotal");
            map.put("profit", saleTotal - purchasingTotal);
        }
        return getSaleDataByMonth;
    }
}
